package com.ruoyi.info.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.info.domain.SysBuilding;
import com.ruoyi.info.mapper.SysBuildingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.info.mapper.SysVillageMapper;
import com.ruoyi.info.domain.SysVillage;
import com.ruoyi.info.service.ISysVillageService;

/**
 * 小区Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-03
 */
@Service
public class SysVillageServiceImpl implements ISysVillageService 
{
    @Autowired
    private SysVillageMapper sysVillageMapper;

    @Autowired
    private SysBuildingMapper sysBuildingMapper;

    /**
     * 查询小区
     * 
     * @param id 小区主键
     * @return 小区
     */
    @Override
    public SysVillage selectSysVillageById(Long id)
    {
        return sysVillageMapper.selectSysVillageById(id);
    }

    /**
     * 查询小区列表
     * 
     * @param sysVillage 小区
     * @return 小区
     */
    @Override
    public List<SysVillage> selectSysVillageList(SysVillage sysVillage)
    {
        return sysVillageMapper.selectSysVillageList(sysVillage);
    }

    /**
     * 新增小区
     * 
     * @param sysVillage 小区
     * @return 结果
     */
    @Override
    public int insertSysVillage(SysVillage sysVillage)
    {
        return sysVillageMapper.insertSysVillage(sysVillage);
    }

    /**
     * 修改小区
     * 
     * @param sysVillage 小区
     * @return 结果
     */
    @Override
    public int updateSysVillage(SysVillage sysVillage)
    {
        return sysVillageMapper.updateSysVillage(sysVillage);
    }

    /**
     * 批量删除小区
     * 
     * @param ids 需要删除的小区主键
     * @return 结果
     */
    @Override
    public int deleteSysVillageByIds(Long[] ids)
    {
        return sysVillageMapper.deleteSysVillageByIds(ids);
    }

    /**
     * 删除小区信息
     * 
     * @param id 小区主键
     * @return 结果
     */
    @Override
    public int deleteSysVillageById(Long id)
    {
        return sysVillageMapper.deleteSysVillageById(id);
    }

    @Override
    public List<SysVillage> queryTree(SysVillage sysVillage,SysBuilding sysBuilding) {

        List<SysVillage> allVillages = sysVillageMapper.selectSysVillageList(sysVillage);
        Map<Long,SysVillage> allVillageMap = new HashMap<>();
        for (SysVillage village : allVillages) {
            allVillageMap.put(village.getId(),village);
            List<SysBuilding> buildings = sysBuildingMapper.selectSysBuildingList(sysBuilding);
            for (SysBuilding building : buildings) {
                if (building.getVillageId() == null){
                    buildings.add(building);
                }else{
                    Long villageId = building.getVillageId();
                    SysVillage parent = allVillageMap.get(villageId);
                    if (parent != null){
                       parent.getChildren().add(building);
                    }
                }
            }
        }
        return allVillages;
    }
}
